<template>
	<xn-form-container
		:title="dataRef.reduce == 1 ? '添加余额' : '扣除余额'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="额度(小时)：" name="addReduce">
				<a-input type="Number" v-model:value="formData.addReduce" placeholder="请输入额度" allow-clear />
			</a-form-item>
			<a-form-item label="生效日期：" name="effectiveDate" v-if="dataRef.reduce == 1">
				<a-date-picker
					v-model:value="formData.effectiveDate"
					value-format="YYYY-MM-DD"
					placeholder="请选择开始日期"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="失效日期：" name="expirationDate" v-if="dataRef.reduce == 1">
				<a-date-picker
					v-model:value="formData.expirationDate"
					value-format="YYYY-MM-DD"
					placeholder="请选择开始日期"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="备注：" name="reason">
				<a-textarea
					v-model:value="formData.reason"
					placeholder="请输入备注"
					:auto-size="{ minRows: 2, maxRows: 5 }"
					allow-clear
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="sysShiftManageForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysShiftManageApi from '@/api/biz/sysShiftManageApi'
	import sysHolidayAccountDetailedApi from '@/api/biz/sysHolidayAccountDetailedApi'
	import moment from 'moment'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const dataRef = ref({})

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		dataRef.value = record
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		addReduce: [required('请输入要增加的额度')],
		effectiveDate: [required('请选择生效日期')],
		expirationDate: [required('请选择失效日期')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.reduce = dataRef.value.reduce
			formDataParam.leaveType = dataRef.value.leaveType
            formDataParam.holidayId = dataRef.value.holidayId
			sysHolidayAccountDetailedApi
				.sysHolidayAccountDetailedSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
